<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2016/5/23
 * Time: 9:08
 */

namespace Admin\Controller;



class StudentController extends BaseController{



    public function index(){
        $depart = D('Depart');
        $Student = D('Student');
        $list = array();
        $xys = $depart->getAllXyname();

        foreach($xys as $xy){

            $zys = $depart->getAllXyById($xy['id']);

            foreach($zys as $zy){
                $classes = $depart->getAllBjById($zy['id']);
                foreach($classes as $class){
                    $name = $zy['name'].'-'.$class['name'];
                    $id= $class['id'];
                    $count = $Student->getCountByWhere("c_id=".$id);
                    $list[] = array("id"=>$id,"name"=>$name,'count'=>$count);
                }
            }
        }

        $this->assign('list',$list);

        $this->display();
    }

    public function manage(){
        $Student = D('Student');
        $c_id = $_GET['id'];
        $map = "c_id=".$c_id;
        $limit = \Api\Libs\PageUtil::getLimit(10);
        $page = \Api\Libs\PageUtil::getPageHtml($Student->getCountByWhere($map),10);
        $studentList = $Student->getAllStudentWhere($map,$limit);
        $this->assign('studentList',$studentList);
        $this->assign('page',$page);
        $this->assign('c_id',$c_id);
        $this->display();
    }

    public function add(){
        if(!IS_POST){
            $data['id'] = $_GET['id'];
            $Student = D('Student');
            $S = $Student->getXy($data['id']);
            $ID = $Student->getByAllId($data['id']);
            $this->assign('S',$S);
            $this->assign('ID',$ID);
            $this->display();
        }else{

            $this->isPost();//验证是否是post方式提交数据
            $data['d_id'] = $_POST['x_id'];
            $data['p_id'] = $_POST['z_id'];
            $data['c_id'] = $_POST['c_id'];
            $data['num'] = $_POST['num'];
            $data['name'] = $_POST['name'];
            if (($flag = \Api\Libs\HttpVerifyUtil::verifyFormParamNotNull($data)) !== '000000')
                $this->out('RESULT_FAIL',\Api\Libs\ExplainRespCodeUtil::explain($flag));
            $student = D('Student');
            $yz = $student->getYz($data);//验证学号是否存在
            if ($yz == '000000')
                $this->out('RESULT_FAIL','学号重复！');
            $flag = $student->addStudent($data);
            if ($flag === '000000')
                $this->out('RESULT_OK','',__CONTROLLER__);
            $this->out('RESULT_FAIL',\Api\Libs\ExplainRespCodeUtil::explain($flag));
        }
    }

    public function del(){
        $id = $_REQUEST['id'];
        if (($flag = \Api\Libs\HttpVerifyUtil::verifyFormParamNotNull($id)) !== '000000')
            $this->out('RESULT_FAIL',\Api\Libs\ExplainRespCodeUtil::explain($flag));
        $student = D ('Student');
        $flag = $student->getDelStudent($id);
        if ($flag !== false)
            $this->out('RESULT_OK','',__CONTROLLER__);
        $this->out('RESULT_FAIL',\Api\Libs\ExplainRespCodeUtil::explain($flag));
    }

    public function edit(){
        if(!IS_POST){
            $data['id'] = $_GET['id'];
            if (($flag = \Api\Libs\HttpVerifyUtil::verifyFormParamNotNull($data)) !== '000000')
                $this->error(\Api\Libs\ExplainRespCodeUtil::explain($flag));
            $Depart = D('Depart');
            $student = D('Student');
            $S = $student->getStudentById($data);

            if (empty($S)) $this->error('不存在该记录');
            $XyList = $Depart->getAllXyname();

            $ZyList = $Depart->getAllXyById($S['d_id']);
            $BjList = $Depart->getAllBjById($S['p_id']);
            $this->assign('XyList',$XyList);
            $this->assign('ZyList',$ZyList);
            $this->assign('BjList',$BjList);
            $this->assign('S',$S);
            $this->display();
        }else{
            $this->isPost();//验证是否是post方式提交数据
            $data['id'] = $_POST['id'];
            $data['d_id'] = $_POST['x_id'];
            $data['p_id'] = $_POST['z_id'];
            $data['c_id'] = $_POST['c_id'];
            $data['num'] = $_POST['num'];
            $data['name'] = $_POST['name'];
            $student = D('Student');
            $yz = $student->getYz($data);//验证学号是否存在
            if ($yz == '000000')
                $this->out('RESULT_FAIL','学号重复！');
            if (($flag = \Api\Libs\HttpVerifyUtil::verifyFormParamNotNull($data)) !== '000000')
                $this->out('RESULT_FAIL',\Api\Libs\ExplainRespCodeUtil::explain($flag));
            $Student = D('Student');
            $flag = $Student->editStudent($data);
            if ($flag === '000000')
                $this->out('RESULT_OK','',__CONTROLLER__);
            $this->out('RESULT_FAIL',\Api\Libs\ExplainRespCodeUtil::explain($flag));
        }
    }





    public function getBJ(){

        if(empty($_GET['z_id'])){
            $data['status']  = 1;
            $this->ajaxReturn(['status'=>0,'msg'=>'error: x_id is not found!']);
            die();
        }
        $z_id = $_GET['z_id'];
        $Depart = D('Depart');
        $res = $Depart->getAllBjById($z_id);

        $this->ajaxReturn(['status'=>1,'data'=>$res]);
    }


    /**
     * 导出学生记录
     */
    public function export() {
        $Student = D('Student');
        $xlsData = $Student->getAllStudentWhere();
        $xlsName = "学生表";
        //echo $xlsName;die;
        $xlsCell = array (
            array('id','序号'),
            array('num','学号'),
            array('name', '姓名'),
            array('d_name','学院'),
            array('p_name','专业'),
            array('c_name','班级')
        );

        exportExcel($xlsName,$xlsCell,$xlsData);//表名，列标题、表数据
    }

    public function search(){
        if(empty($_POST['data'])){
            //未传入数据
            $this->ajaxReturn(['errMsg'=>'没有传入数据','status'=>0],'JSON');
            exit();
        }
        $student = D('Student');
        $res =  $student->search($_POST['data']);

        if(!$res){
            //没有找到
            $this->ajaxReturn(['errMsg'=>$student->getError(),'status'=>0],'JSON');
            exit();
        }
        $this->ajaxReturn(['data'=>$res,'status'=>1],'JSON');
    }
    /**
     * 导入学生数据
     */
    public function import(){
        if (!IS_POST) {
            // 与下条语句相同   $id = $_REQUEST['id'];
            $c_id = $_REQUEST['id'];
            if (empty($c_id)) $this->error('非法访问');
            $Student = D ('Student');
            if(!$Student->hasClass($c_id)){
                $this->error('班级不存在');
            }
            $className = $Student->getFullClassName($c_id);
            $this->assign('className',$className);
            $this->assign('classId',$c_id);
            $this->display();
        } else {
            $class_id = I ('post.class_id');
            if (empty($class_id)) $this->error('非法访问');

            $Student = D("Student");
            $All_id = $Student->getByAllId($class_id);
            if(isset($_FILES["file"]) && ($_FILES["file"]["error"] == 0)){
                $tempName = APP_PATH.'Runtime/Temp/'.uniqid().'.'.pathinfo($_FILES['file']['name'],PATHINFO_EXTENSION);

                move_uploaded_file($_FILES["file"]["tmp_name"], $tempName);

                $result = importExecl($tempName);

                @unlink($tempName);
                if($result["error"] == 1){
                    // 获取数据内容
                    $execl_data = $result["data"][0]["Content"];

                    $count = $execl_data["data"][0]["Rows"];
                    $successCount = $failCount = $repeatCount = 0;

                    foreach($execl_data as $k=>$v){
                        if($k != 1){  // 第一行为标题

                            $data['num'] = $v[0];
                            $data['name'] = $v[1];
                            $data['d_id'] = $All_id['x_id'];
                            $data['p_id'] = $All_id['z_id'];
                            $data['c_id'] = $All_id['c_id'];


                            $flag = $Student->import_add($data);

                            switch ($flag) {
                                case '0':
                                    $successCount ++;
                                    break;
                                case '1':
                                    $failCount ++;
                                    break;
                                case '3':
                                    $repeatCount ++;
                                    break;
                            }
                        }
                    }

                    $this->success('导入学生结束，其中成功导入 '.$successCount.' 人'.'，失败 '.$failCount.' 人，'.'冲突 '.$repeatCount.' 人', __CONTROLLER__,4);
                } else {

                    $this->error('Excel表格数据错误');
                }
            } else {
                $this->error('请先选择一个Excel文件');
            }
        }
    }



}